package com.junxonline.instant.admin.controller.device;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junxonline.instant.admin.service.config.OrganizationService;
import com.junxonline.instant.admin.service.config.RegionService;
import com.junxonline.instant.admin.service.device.*;
import com.junxonline.instant.common.annotation.DeviceOperationLog;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.entity.common.CommonResultAll;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import com.junxonline.instant.dao.entity.model.config.Region;
import com.junxonline.instant.dao.entity.model.device.Group;
import com.junxonline.instant.dao.entity.model.device.Terminal;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.device.BatchTerminalVO;
import com.junxonline.instant.dao.entity.vo.device.TerminalVO;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Log4j2
@RestController
@RequestMapping(value = "/device/terminal")
public class TerminalController {

    @Autowired
    private TerminalService terminalService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private RegionService regionService;

    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询组织机构树（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端分组管理-查询组织机构树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryOrganizationTree")
    public CommonResult queryOrganizationTree() {
        return CommonResult.success(organizationService.queryOrganizationTree(new QueryWrapper<>()));
    }

    /**
     * 查询终端分组树（TreeSelect）
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端分组树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryGroupTree")
    public CommonResult queryGroupTree(@RequestBody(required = false) TerminalVO vo) {
        return CommonResult.success(groupService.queryGroupTree(vo));
    }

    /**
     * 查询终端分组下拉列表（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端分组下拉列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryGroupTreeSelect")
    public CommonResult queryGroupTreeSelect(@RequestBody(required = false) TerminalVO vo) {
        return CommonResult.success(groupService.queryGroupTreeSelect(new QueryWrapper<Group>().eq("t.organization_id", vo.getOrganizationId())));
    }

    /**
     * 查询城市下拉列表（Select）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询城市下拉列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryCitySelect")
    public CommonResult queryCitySelect() {
        return CommonResult.success(regionService.querySelect(new LambdaQueryWrapper<Region>().eq(Region::getType, 2)));
    }

    /**
     * 查询终端音量设置
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端音量设置", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryVolumeSetting")
    public CommonResult queryVolumeSetting(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.queryVolumeSetting(vo.getId()));
    }

    /**
     * 查询终端音量
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端音量", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryVolume")
    public CommonResult queryVolume(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.queryVolume(vo.getId()));
    }

    /**
     * 查询终端显示方式设置
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端显示方式设置", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryDisplayTypeSetting")
    public CommonResult queryDisplayTypeSetting(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.queryDisplayTypeSetting(vo.getId()));
    }

    /**
     * 查询终端显示方式
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端显示方式", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryDisplayType")
    public CommonResult queryDisplayType(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.queryDisplayType(vo.getId()));
    }

    /**
     * 查询终端截屏
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端截屏", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryScreenshot")
    public CommonResult queryScreenshot(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.queryScreenshot(vo.getId()));
    }

    /**
     * 查询终端指令设置
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端指令设置", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryCommandSetting")
    public CommonResult queryCommandSetting(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.queryCommandSetting(vo.getId()));
    }


    /**
     * 查询终端列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<TerminalVO> vo) {
        return CommonResult.success(terminalService.queryList(vo, new QueryWrapper<>()));
    }

    /**
     * 查询终端
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:query")
    @OperationLog(label = "终端管理-查询终端", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(terminalService.query(vo.getId()));
    }

    /**
     * 编辑终端
     *
     * @param terminal 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:edit")
    @OperationLog(label = "终端管理-编辑终端", type = OperationEnum.EDIT)
    @DeviceOperationLog(label = "编辑终端", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Terminal terminal) {
        if (terminal == null || terminal.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        terminalService.edit(terminal);
        return CommonResult.success();
    }

    /**
     * 批量编辑终端
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:edit")
    @OperationLog(label = "终端管理-批量编辑终端", type = OperationEnum.EDIT)
    @DeviceOperationLog(label = "批量编辑终端", type = OperationEnum.EDIT)
    @PostMapping(value = "/batchEdit")
    public CommonResult batchEdit(@RequestBody(required = false) BatchTerminalVO vo) {
        if (vo == null || vo.getIds() == null || vo.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        terminalService.batchEdit(vo);
        return CommonResult.success();
    }

    /**
     * 删除终端
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:delete")
    @OperationLog(label = "终端管理-删除终端", type = OperationEnum.DELETE)
    @DeviceOperationLog(label = "编辑终端", type = OperationEnum.EDIT)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> vo) {
        if (vo == null || vo.getIds() == null || vo.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        terminalService.delete(vo.getIds());
        return CommonResult.success();
    }

    /**
     * 导出
     *
     * @param vo 请求参数
     * @return CommonResultAll
     */
    @RequiresPermissions(value = "device:terminal:export")
    @OperationLog(label = "终端管理-导出", type = OperationEnum.EXPORT)
    @PostMapping(value = "/exportExcel")
    public CommonResult exportExcel(@RequestBody(required = false) IdsVO<List<Long>> vo) {
        return CommonResultAll.success(terminalService.exportExcel(vo));
    }

    /**
     * 导入
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:terminal:import")
    @OperationLog(label = "终端管理-导入", type = OperationEnum.IMPORT, isSaveParams = false)
    @PostMapping(value = "/importExcel")
    public CommonResult importExcel(@RequestBody(required = false) IdsVO<List<Object>> vo) {
        if (vo == null || vo.getIds() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }

        JSONArray array = (JSONArray) JSONArray.toJSON(vo.getIds());
        return CommonResult.success(terminalService.importExcel(array));
    }
}